<?php

namespace App\Models;

class HotSearchs extends Base
{

    protected $table = 'hot_searchs';

    protected $fillable = [
        'id','content','count','type','status','created_at','updated_at'
    ];

    /**
     * 列表数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 条数
     * @return mixed
     */
    protected function getList($params = [] , $fields = ['*'] , $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                $query ->where('status','<>',-1);
            if(isset($params['type']) && $params['type']) {
                $query ->where('type',$params['type']);
            }
        })->orderBy('count','DESC')->limit($limit)->get();
        return $lists;
    }

    /**
     * 新增词条
     * @param array $params 条件参数
     * @return mixed
     */
    protected function insertSearch($params = []){
        $where['content'] = $params['content'];
        $where['type'] = $params['type'];
        $id = $this->where($where)->value('id');
        if($id > 0){
            $this->where('id',$id)->increment('count',1);
        }else{
            $this->create($params);
        }
        return true;
    }
}
